package com.xiaoke.liantongdemo.widget;

import android.content.Context;
import android.text.InputFilter;
import android.text.InputFilter.LengthFilter;
import android.view.Gravity;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.xiaoke.liantongdemo.bean.BusinessInput;

public class BusinessInputLayout extends LinearLayout {
	
	private Context context;
	private BusinessInput businessInput;
	
	private EditText et;
	private Spinner sp; 

	public BusinessInputLayout(Context context, BusinessInput businessInput) {
		super(context);
		this.context = context;
		this.businessInput = businessInput;
		init();
	}
	
	private void init(){
		int type = businessInput.getType();
		if(type == BusinessInput.Type.SIGN){
			return;
		}
		TextView tvDesc = new TextView(context);
		tvDesc.setTextColor(0xff000000);
//		tvDesc.setGravity(Gravity.CENTER);
		tvDesc.setText(businessInput.getDesc());
		LayoutParams lpDesc = new LayoutParams(200, 40);
		addView(tvDesc, lpDesc);		
		if(type == BusinessInput.Type.TEXT){
			et = new EditText(context);
//			LengthFilter filter = new LengthFilter(businessInput.getMaxLength());
//			et.setFilters(new InputFilter[]{filter});
			LayoutParams lpEt = new LayoutParams(400, 40);
			addView(et, lpEt);
		}else if(type == BusinessInput.Type.CHOICE){
			sp = new Spinner(context);
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, 
					android.R.layout.simple_spinner_item, businessInput.getValues());
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			sp.setAdapter(adapter);
			LayoutParams lpSp = new LayoutParams(400, 40);
			addView(sp, lpSp);
		}
		
	}
	
	public String getInput(){
		int type = businessInput.getType();
		if(type == BusinessInput.Type.TEXT){
			return et.getText().toString();
		}else if(type == BusinessInput.Type.CHOICE){
			return sp.getSelectedItem().toString();
		}
		return null;
	}

}
